var fs = require('fs');
var http = require('http');
var https = require('https');
const C = require('./helper/core')
// var privateKey = fs.readFileSync('./sslcertNoCa/2_lechouchou.club.key', 'utf8');
// var certificate = fs.readFileSync('./sslcertNoCa/1_lechouchou.club_bundle.crt', 'utf8');
var ca = fs.readFileSync('./sslcert/1_root_bundle.crt', 'utf8');
var privateKey = fs.readFileSync('./sslcert/3_lechouchou.club.key', 'utf8');
var certificate = fs.readFileSync('./sslcert/2_lechouchou.club.crt', 'utf8');

var credentials = {key: privateKey, cert: certificate, ca: ca};
// var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

// your express configuration here
var bodyParser = require('body-parser');
var mySession = require('./mySession')
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(mySession({
    expireTime: 300000 //6 minute
}))
app.use('/test', require('./controllers/test'))
app.use('/yjc', require('./controllers/yjc'))
app.use('/zhuajiu', require('./controllers/zhuajiu'))

//缓存用户信息
app.post('/setUser',(req,res) => {
    var user = req.body.user
    req.session.user = user
    //console.log('user.avataURL: '+user.avatarUrl)
    res.end()
})


//用户是否登录
app.all('/is_logined',(req,res) => {
    res.json({
        ok: C.is_logined(req)
    })
})


var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(80);
httpsServer.listen(443);
// httpsServer.listen(80);
